<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:team:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:team:remove']"
        >删除</el-button>
      </el-col>
      <div style="float:right;">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="getList"
        >刷新</el-button>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable="checkSelect"/>
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="是否负责人" align="center" prop="isLeader">
        <template slot-scope="scope">
          <span v-if="scope.row.isLeader == 1">是</span>
          <span v-if="scope.row.isLeader == 0">否</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:team:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:team:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改团队，针对客户或商机进行服务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-input v-model="form.teamType" type="hidden"/>
        <el-input v-model="form.businessId" type="hidden"/>
        <el-form-item label="请选择团队成员" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择团队成员" filterable>
            <el-option label="请选择团队成员" value="" />
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.userName"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeam, getTeam, delTeam, addTeam, updateTeam, exportTeam, getNotTeamUser } from "@/api/crm/team/team";

export default {
  props: {
    //团队类型
    teamType: "",
    //业务id
    businessId: ""
  },
  name: "Team",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 团队，针对客户或商机进行服务表格数据
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      //用户列表
      userList: [],
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "团队成员不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    
  },
  methods: {
    /** 负责人不允许选中 */
    checkSelect (row, index) {
      let isChecked = true;
      if (row.isLeader === 1) { // 判断是否负责人
        isChecked = false
      } else {
         isChecked = true
      }
      return isChecked
    },
    /** 查询团队，针对客户或商机进行服务列表 */
    getList() {
      let params = {teamType: this.teamType, businessId: this.businessId};
      this.loading = true;
      listTeam(params).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamType: null,
        businessId: null,
        userId: null,
        isLeader: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.teamType = this.teamType;
      this.form.businessId = this.businessId;
      this.open = true;
      this.title = "添加团队成员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改团队成员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeam(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              getNotTeamUser({teamType: this.teamType, businessId: this.businessId}).then(response => {
                this.userList = response;
              });
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(1 === row.isLeader){
        this.msgError("负责人不允许删除，请到主页面进行修改！");
      }else{
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除团队编号为"' + ids + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delTeam(ids);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
            getNotTeamUser({teamType: this.teamType, businessId: this.businessId}).then(response => {
              this.userList = response;
            });
          })
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有团队，针对客户或商机进行服务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTeam(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  },
  watch: {
      businessId: {
      　　handler(newValue, oldValue) {
      　　    if(null != newValue){
                getNotTeamUser({teamType: this.teamType, businessId: this.businessId}).then(response => {
                  this.userList = response;
                });
                this.getList();
              }
      　　},
          deep: true,
      　　immediate: true
      }
  } 
};
</script>
